package com.example.demo.repo;

import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

// 这里用map模拟库存，不走DB
@Repository
public class DBRepertoryRepo implements RepertoryRepo {

    private Map<Long, Integer> repertories = new ConcurrentHashMap<>();
    {
        // 初始化库存
        repertories.put(11L, 5);
        repertories.put(12L, 3);
        repertories.put(13L, 10100);
        repertories.put(14L, 10020);
        // ...
    }

    @Override
    public Integer takeOut(Long productId, Integer numbers) {
        Integer repo = getNumbers(productId);
        repo = repo - numbers;
        repertories.put(productId, repo);
        return repo;
    }

    @Override
    public Integer getNumbers(Long productId) {
        return repertories.get(productId);
    }

    @Override
    public Map<Long, Integer> getRepos() {
        return new HashMap<>(repertories);
    }
}
